package com.tanhua.server.service;

import cn.hutool.core.collection.CollUtil;
import com.alibaba.dubbo.config.annotation.Reference;
import com.tanhua.dubbo.server.api.QuanZiApi;
import com.tanhua.dubbo.server.pojo.Publish;
import com.tanhua.dubbo.server.pojo.Users;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.concurrent.CompletableFuture;

@Service
public class TimeLineService {

    @Reference
    private QuanZiApi quanZiApi;

    //将此方法改为异步执行
    @Async
    public CompletableFuture<String> saveTimeLine(Long id, Publish publish) {
       try {
           List<Users> usersList=quanZiApi.queryFriendIds(id);
           List<Object> friendIds = CollUtil.getFieldValues(usersList, "friendId");
           //遍历每一个好友，对时间线表添加数据
           for (Object friendId : friendIds) {
               quanZiApi.saveTimeLine(friendId,publish);
           }
       }catch (Exception e){
           return CompletableFuture.completedFuture("error");//异步执行失败
       }
       return CompletableFuture.completedFuture("ok");//表示异步执行成功
    }
}
